﻿(function ($) {
    $.rolejs = {
        // 角色所属客户 
        initCustomerInfo: function (CustomerName) {

            $("#CustomName").empty();
           
                var optionstring = "";
                $.ajax({
                    url: "./RoleInfo.ashx",
                    data: {
                        Action: "initCustomerInfo",
                        CustomerName: CustomerName
                    },
                    async: true,
                    type: "post",
                    datatype: "json",
                    success: function (data) {
                        Loading(false);
                        data = JSON.parse(data);
                        console.log(data);
                        if (data.length > 0) {
                            for (i in data) {
                                optionstring += "<option value=\"" + data[i].ID + "\" >" + data[i].CustomName.trim() + "</option>";
                            }
                            if (CustomerName == "宏大") {
                                $("#CustomName").html("<option value=''>请选择</option>" + "<option value='0'>宏大</option>" + optionstring);
                            }
                            else {
                                $("#CustomName").html("<option value=''>请选择</option>"  + optionstring);
                            }                            
                        }
                        else
                            dialogMsg(data.msg, -1);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        Loading(false);
                        dialogMsg(errorThrown, -1);
                    }
                });
           
        },

        // 保存新增角色信息
        saveRoleInfo: function (RoleName, CustomName, Remark, selectid,grid) {

            $.ajax({
                url: "./RoleInfo.ashx",
                data: JSON.stringify({
                    Action: "addRoleInfo",
                    RoleName: RoleName,
                    CustomName: CustomName,
                    Remark: Remark,
                    quanxian: selectid
                }),
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                       
                    }
                    else if (data== "falut") {
                        dialogMsg("保存失败", -1);
                    }
                    else if (data == "hasexit") {
                        dialogMsg("已存在此角色", 0);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        // 删除角色信息
        deleteRoleInfo: function (RoleID, grid) {
            $.ajax({
                url: "./RoleInfo.ashx",
                data: {
                    Action: "deleteRoleInfo",
                    RoleID: RoleID
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    console.log(data);
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("删除成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                    }
                    else if (data == "falut") {
                        dialogMsg("删除失败", -1);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        ///根据角色ID得到角色信息
        getRoleInfoByID: function (RoleID) {

            var t = $("#tree");
            t = $.fn.zTree.init(t, setting, zNodes);
            demoIframe = $("#testIframe");
            demoIframe.bind("load", loadReady);

            var node = t.getNodes();
            var nodes = t.transformToArray(node);

            $.ajax({
                url: "./RoleInfo.ashx",
                data: {
                    Action: "getRoleInfoByID",
                    RoleID: RoleID
                },
                type: "post",
                dataType: "json",
                async: false,
                success: function (data) {
                    if (data == null) return false;
                    console.log(data);
                    //data = JSON.parse(data);
                    $.each(data, function (i, row) {
                        $("#RoleName").val(row.RoleName);
                        $("#CustomName").val(row.CustomName);
                        $("#Remark").val(row.Remark);

                        for (var i = 0; i < nodes.length; i++) {
                            if (nodes[i].id.trim() == row.MenuNo.trim()) {
                                nodes[i].checked = true;
                                t.updateNode(nodes[i]);
                            }
                        }
                    });
                    Loading(false);
                }, beforeSend: function () {
                    Loading(true);
                }
            });
        },

        //保存编辑角色信息
        editRoleInfo: function (RoleID, RoleName, Remark, selectid, grid) {

            $.ajax({
                url: "./RoleInfo.ashx",
                data: JSON.stringify({
                    Action: "editRoleInfo",
                    RoleID: RoleID,
                    RoleName: RoleName,
                    Remark: Remark,
                    quanxian: selectid
                }),
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();

                    }
                    else if (data == "falut") {
                        dialogMsg("保存失败", -1);
                    }
                    else if (data == "hasexit") {
                        dialogMsg("已存在此角色", 0);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },


        //新增角色页面  当是宏大用户登录 构造权限树
        initRoleLimitByHongda: function (page, RoleID) {
           
            $(document).ready(function () {
                $.ajax({
                    url: "../Menu/GetMenuList.ashx?Action=GetLimitTree",
                    type: "post",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        zNodes = eval(data);
                        //console.log(zNodes);
                    },
                    error: function () {
                        alert("Ajax请求数据失败!");
                    }
                });

                var t = $("#tree");
                t = $.fn.zTree.init(t, setting, zNodes);
                console.log(zNodes);
                demoIframe = $("#testIframe");
                demoIframe.bind("load", loadReady);
                if(page = 'EDIT'){
                    $.rolejs.getRoleInfoByID(RoleID);
                }
            });

        },


        //新增角色页面  当是客户登录 构造权限树
        initRoleLimitByCustomer: function (page, RoleID,CustomerName) {
           

            $(document).ready(function () {
                $.ajax({
                    url: "../Menu/GetMenuList.ashx?Action=initRoleLimitByCustomer",
                    data: {
                        CustomerName: CustomerName
                    },
                    type: "post",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        zNodes = eval(data);
                        //console.log(zNodes);
                    },
                    error: function () {
                        alert("Ajax请求数据失败!");
                    }
                });

                var t = $("#tree");
                t = $.fn.zTree.init(t, setting, zNodes);
                console.log(zNodes);
                demoIframe = $("#testIframe");
                demoIframe.bind("load", loadReady);
                if (page = 'EDIT') {
                    $.rolejs.getRoleInfoByID(RoleID);
                }
            });

        }
    };
   
    
    $(function () {
        
    });
})(jQuery);